﻿using TextExtractionMaster;
using Microsoft.Office.Interop.Word;

namespace TextExtractionMaster.Extractor
{
    public class WordDocTextExtractor : ITextExtractor
    {
        public string ExtractText(string filePath)
        {
            Application wordApp = new Application();
            Document doc = null;
            string docText = string.Empty;

            try
            {
                // 打开文档
                doc = wordApp.Documents.Open(filePath);

                // 提取文本
                docText = doc.Content.Text;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                // 关闭文档
                if (doc != null)
                {
                    doc.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
                }

                // 退出 Word 应用
                wordApp.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
            }

            return docText;
        }
    }
}